import { locations } from "../../../gpu/locations";
const PointsGeometry =/*wgsl*/`
#include <Camera>
#include <Trans_def>

 
 struct Vertex {
    @builtin(vertex_index) VertexID:u32,
    #if ~{def.buffer}
    @location(${locations.position[1]}) position : vec3<f32>,
    #endif
    #if ~{def.buffer}
    @location(${locations.size[1]}) size : f32,
    #endif
    #if ~{def.buffer}
    @location(${locations.color[1]}) color : vec3<f32>,
    #endif
    #if ~{def.buffer}
    @location(${locations.rotation[1]}) rotation : f32,
    #endif 
    #if ~{def.buffer}
    @location(${locations.opacity[1]}) opacity : f32,
    #endif 
    // #if ~{def.visible}
    // @location(${locations.visible[1]}) visible : f32,
    // #endif 
  };
  struct VertexOutput {
    @builtin(position) Position : vec4<f32>,
    @location(0) localPos:vec3<f32>,
    #if ~{def.map}
    @location(1) texcoord:vec2<f32>,
    #endif
    @location(2) color:vec4<f32>,
    #if ~{def.logarithmicDepth}
    @location(3) flogz : f32,
    #endif
  }
  @vertex
  fn main(vertexInput:Vertex) -> VertexOutput {
        var output:VertexOutput;
        var modelMatrix:mat4x4<f32>;
        var u_NormalMatrix:mat4x4<f32>;
        // 从trans中分解处模型矩阵与法向量矩阵
        #if ~{def.trans}
        modelMatrix=trans.modelMat;
        u_NormalMatrix=trans.normalMat;
        #endif
        #if ~{def.transinstance}
        modelMatrix=trans[vertexInput.index].modelMat;
        u_NormalMatrix=trans[vertexInput.index].normalMat;
        #endif
        output.color = vec4(vertexInput.color, vertexInput.opacity);
        let points = array(
          vec2f(-1, -1),
          vec2f( 1, -1),
          vec2f(-1,  1),
          vec2f(-1,  1),
          vec2f( 1, -1),
          vec2f( 1,  1),
        );
        let pos = points[vertexInput.VertexID];
        let c = cos(vertexInput.rotation);
        let s = sin(vertexInput.rotation);
        let rot = mat2x2f(
          c, s,
         -s, c,
       );
      
        var clipPos =camera.vp* (modelMatrix * vec4f(vertexInput.position,1) );
        var pointPos=vec4f( rot * pos *  vertexInput.size , 0, 0);;
        output.Position =clipPos+pointPos;
        #if ~{def.map}
            // 假设我们要将法线（Z方向）向上调整至模型的Y轴  
        // var upVector = vec3<f32>(0.0, 1.0, 0.0);  
        // var rightVector = normalize(cross(upVector, vec3<f32>(0.0, 0.0, 1.0)));  
        // var forwardVector = normalize(cross(rightVector, upVector)); 
        // output.texcoord = rot*pos * 0.5 + 0.5;
        output.texcoord = pos * 0.5 + 0.5; // 直接使用正方形的归一化坐标作为纹理坐标
        // output.texcoord = vec2<f32>(0.5, 0.5) + vec2<f32>(0.0, -0.5); // 这里根据需要调整 
        #endif
        output.localPos=clipPos.xyz/clipPos.w;
                // 对数深度缓冲
       #if ~{def.logarithmicDepth}
       output.flogz = 1.0 + output.Position.w;
      #endif
        return output;
      }
`
export { PointsGeometry }